package com.hwl.eduservice.controller.admin;


import com.hwl.commonutils.result.RespBean;
import com.hwl.eduservice.pojo.Chapter;
import com.hwl.eduservice.service.ChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author long
 * @since 2021-09-21
 */
@Api(tags = "章节管理")
@RestController
@RequestMapping("/admin/eduChapter")
public class ChapterController {

    @Resource
    private ChapterService chapterService;

    @ApiOperation("新增章节")
    @PostMapping("save")
    public RespBean save(@RequestBody Chapter chapter) {
        if (chapterService.save(chapter)) {
            return RespBean.ok().message("新增章节成功！");
        }
        return RespBean.error().message("新增章节失败！");
    }

    @ApiOperation("根据id查询章节")
    @GetMapping("get/{chapterId}")
    public RespBean getById(@PathVariable String chapterId) {
        return RespBean.ok().data(chapterService.getById(chapterId));
    }

    @ApiOperation("修改章节")
    @PutMapping("update")
    public RespBean updateChapter(@RequestBody Chapter chapter) {
        if (chapterService.updateById(chapter)) {
            return RespBean.ok().message("更新章节成功！");
        }
        return RespBean.error().message("更新章节失败！");
    }

    @ApiOperation("删除章节")
    @DeleteMapping("remove/{chapterId}")
    public RespBean removeById(@PathVariable String chapterId) {
        if (chapterService.removeChapterById(chapterId)) {
            return RespBean.ok().message("删除章节成功！");
        }
        return RespBean.ok().message("删除章节失败！");
    }

    @ApiOperation("获取章节和小节列表")
    @GetMapping("getAllChapter/{courseId}")
    public RespBean getAllChapter(@PathVariable String courseId) {
        return RespBean.ok().data(chapterService.getAllChapter(courseId));
    }

}

